package org.one.afternoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.Mine;
import org.one.afternoon.domain.Movie;
import org.one.afternoon.service.MineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/mine")
public class MineController {
    @Autowired
    private MineService mineService;

    @CrossOrigin
    @PostMapping("/query")
    @ResponseBody
    public Result<List<Mine>> query(@RequestBody Mine mine) {
        LambdaQueryWrapper<Mine> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(mine.getPhone()),Mine::getPhone,mine.getPhone())
                .like(StringUtils.isNotBlank(mine.getType()),Mine::getType,mine.getType())
                .like(StringUtils.isNotBlank(mine.getTitle()),Mine::getTitle,mine.getTitle());
        List<Mine> list = mineService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @CrossOrigin
    @PostMapping("/insertOne")
    @ResponseBody
    public Result<String> addOne(@RequestBody Mine mine) {
        boolean save = mineService.save(mine);
        if (!save) {
            return Result.error("添加用户失败");
        } else {
            return Result.success("添加用户成功");
        }
    }

    @CrossOrigin
    @PostMapping("/deleteOne")
    @ResponseBody
    public Result<String> deleteOne(@RequestBody Mine mine) {
        LambdaQueryWrapper<Mine> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(mine.getPhone()), Mine::getPhone, mine.getPhone())
                .eq(StringUtils.isNotBlank(mine.getTitle()), Mine::getTitle, mine.getTitle())
                .eq(StringUtils.isNotBlank(mine.getType()), Mine::getType, mine.getType());
        mineService.remove(lambdaQueryWrapper);
        return Result.success("删除成功");
    }

    @CrossOrigin // 跨域访问
    @PostMapping("/mines")
    @ResponseBody
    public Result<List<Movie>> findAllMines(@RequestBody Mine mine) {

        List<Movie> allMines = mineService.findAllMines(mine);
        System.out.println(allMines);
        return Result.success(allMines);
    }
}
